#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
    pid_t pid = fork();
    if (pid < 0) {
        perror("fork failed");
        exit(1);
    } else if (pid == 0) {
        // 子进程打印消息并等待5秒后退出
        printf("i am child process\n");
        sleep(5);
        exit(42); // 子进程退出码设为42
    } else {
        // 父进程等待子进程结束并分析状态
        int status;
        waitpid(pid, &status, 0);
        if (WIFEXITED(status)) {
            printf("Exit code: %d\n", WEXITSTATUS(status));
            printf("Exit signal: 0\n");
            printf("Core dump flag: 0\n");
        } else if (WIFSIGNALED(status)) {
            printf("Exit signal: %d\n", WTERMSIG(status));
            printf("Core dump flag: %d\n", WCOREDUMP(status));
            printf("Exit code: Not applicable\n");
        } else {
            printf("Child process did not exit normally\n");
        }
    }
    return 0;
}